/**
 * Project: springyounger.lottery.web
 * 
 * File Created at 2010-3-23
 * $Id$
 * 
 * Copyright 2009 Zjut Croporation Limited.
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * Zjut Edu. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with Zjut Edu.
 */
package com.springyouner.lottery.tools;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.WebUtils;

import com.springyouner.lottery.framework.UserSession;

/**
 * 用户拦截器
 * 
 * @author springyounger
 */
public class SingonInteceptor extends HandlerInterceptorAdapter {

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                             Object handler) throws Exception {
        UserSession userSession = (UserSession) WebUtils
                .getSessionAttribute(request, "userSession");
//        int privilege=0;
//        if(null!=userSession){
//            privilege = userSession.getUser().getUserid();
//        }
        if (null==userSession) {
            String url = request.getServletPath();
            String query = request.getQueryString();
            ModelAndView modelAndView = new ModelAndView("login");
            if (null == query) {
                modelAndView.addObject("singonForward", url);
            } else {
                modelAndView.addObject("singonForward", url + "?" + query);
            }
            throw new ModelAndViewDefiningException(modelAndView);
        } else {
            return true;
        }
    }

}
